#Dependency

#library packages
library(ggplot2)
library(ggpubr)
library(data.table)

library(optparse)

option_list <- list(
  make_option("--d", default = "", type = "character", help = "input dep file"),
  make_option("--e", default = "", type = "character", help = "input expr file"),
  make_option("--g", default = "", type = "character", help = "gene name"),
  make_option("--x", default = "", type = "character", help = "x axis"),
  make_option("--y", default = "", type = "character", help = "y axis"),
  make_option("--l", default = "", type = "character", help = "linear regression"),
  make_option("--o", default = "", type = "character", help = "output png file")
)
opt <- parse_args(OptionParser(option_list = option_list))

#import source data
dep<-fread(opt$d) %>%
            as.data.frame()
rownames(dep)<-dep[,1]
dep<-dep[,-1]
expr<-fread(opt$e)  %>%
              as.data.frame()
rownames(expr)<-expr[,1]
expr<-expr[,-1]

#dim parameters and extract corresponding statitstics
gene<-opt$g
choice<-data.frame(x.axis = opt$x, y.axis = opt$y,linear.regression = opt$l )
expr.gene<-data.frame(cell = colnames(dep),expr = as.numeric(expr[gene,]),log2expr = log2(as.numeric(expr[gene,])+1), #修改
                      dep = as.numeric(dep[gene,]))

#plot #修改
#x axis Expression(log-scale TPM) use x=log2expr
if (choice$x.axis == 'Expression(log-scale TPM)') {
  if (choice$linear.regression %in% c('Spearman','Pearson')){
    p<-ggplot(expr.gene,aes(x=log2expr,y=dep))+geom_point(size=1.5)+
      geom_smooth(method = 'lm',formula = y~x,se=F)+
      stat_cor(method = tolower(choice$linear.regression),label.x.npc = 0.68,label.y.npc = 0.95)+
      labs(y="Gene Effect",x="Log2(TPM+1)")+theme_bw()+
      theme(axis.title.x=element_text(face='bold'),
            axis.title.y=element_text(face = 'bold'))
  } else if (choice$linear.regression == 'None') {
    p<-ggplot(expr.gene,aes(x=log2expr,y=dep))+geom_point(size=1.5)+
      labs(y="Gene Effect",x="Log2(TPM+1)")+theme_bw()+
      theme(axis.title.x=element_text(face='bold'),
            axis.title.y=element_text(face = 'bold'))
  }
} else if (choice$x.axis == 'Expression(TPM)') {
  if (choice$linear.regression %in% c('Spearman','Pearson')){
    p<-ggplot(expr.gene,aes(x=expr,y=dep))+geom_point(size=1.5)+
      geom_smooth(method = 'lm',formula = y~x,se=F)+
      stat_cor(method = tolower(choice$linear.regression),label.x.npc = 0.68,label.y.npc = 0.95)+
      labs(y="Gene Effect",x="TPM")+theme_bw()+
      #scale_x_continuous(labels = function(x)format(x,scientific = F,digits = 0))+
      theme(axis.title.x=element_text(face='bold'),
            axis.title.y=element_text(face = 'bold'))
  } else if (choice$linear.regression == 'None') {
    p<-ggplot(expr.gene,aes(x=expr,y=dep))+geom_point(size=1.5)+
      labs(y="Gene Effect",x="TPM")+theme_bw()+
      #scale_x_continuous(labels = function(x)format(x,scientific = F,digits = 0))+
      theme(axis.title.x=element_text(face='bold'),
            axis.title.y=element_text(face = 'bold'))
  }
}
png(file=opt$o,width=2000,height=2000,res=300)
print(p)
dev.off()

#export csv
colnames(expr.gene)[1]<-'cell_line'
write.csv(expr.gene,file = paste('gene.csv',sep = ''),row.names = F)
